//Copyright (C) 2012 Chirila Alexandru
//This file is part of Robo-GoldMiner
//
//Robo-GoldMiner is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//Robo-GoldMiner is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with Robo-GoldMiner.  If not, see <http://www.gnu.org/licenses/>.

package gold;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * This command tries to move the player on room forward in the current
 * direction. 
 * Sintax: move <br />
 * If the new room is a hole, the execute() function will throw
 * {@link EndGame} exception. <br />
 * If the room is either out of bounds, or a wall, the player will 
 * remain at the same position. <br />
 * If the room is clear the player will move to that room. <br />
 * If the room contains a gold coin the player will colect it.
 * @author Alexandru Chirila
 */
public class CommandMove implements UndoCommand, Serializable {

	private static final long serialVersionUID = 922983051690334364L;
	private Maze m;
	private int previousPosition;
	
	public CommandMove(Maze m){
		this.m = m;
	}
	
	@Override
	public boolean execute() throws EndGame {
		previousPosition = m.getPlayer();
		return m.move();
	}

	@Override
	public void undo() {
		m.setPlayer(previousPosition);
	}
	@Override
	public UndoCommand copy() {
		CommandMove clone = new CommandMove(m);
		clone.previousPosition = this.previousPosition;
		return clone;
	}
	

	private void writeObject(ObjectOutputStream stream) throws IOException {
		stream.defaultWriteObject();
	}

	private void readObject(ObjectInputStream stream) throws IOException,
			ClassNotFoundException {
		stream.defaultReadObject();
	}
	
}